package com.example.book.base;

import com.example.book.common.shiro.ShiroUtils;
import com.example.book.common.utils.Constants;
import com.example.book.domain.sys.SysUser;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.Date;
import java.util.UUID;

public class BaseEntity implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 主键
     */
    private String id;

    /**
     * 创建人
     */
    private String createBy;

    /**
     * 创建时间
     */
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd hh:mm:ss")
    private Date createDate;

    /**
     * 更新时间
     */
    private String updateBy;

    /**
     * 更新人
     */
    private Date updateDate;

    /**
     * 备注
     */
    private String remarks;

    /**
     * 删除标记
     */
    private String delFlag;

    /**
     * 分页参数
     * @return
     */

    private Integer pageSize;
    private Integer pageNum;

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }

    public String getCreateBy() {
        return createBy;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setCreateBy(String createBy) {
        this.createBy = createBy;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public String getUpdateBy() {
        return updateBy;
    }

    public void setUpdateBy(String updateBy) {
        this.updateBy = updateBy;
    }

    public Date getUpdateDate() {
        return updateDate;
    }

    public void setUpdateDate(Date updateDate) {
        this.updateDate = updateDate;
    }

    public String getRemarks() {
        return remarks;
    }

    public void setRemarks(String remarks) {
        this.remarks = remarks;
    }

    public String getDelFlag() {
        return delFlag;
    }

    public void setDelFlag(String delFlag) {
        this.delFlag = delFlag;
    }

    /**
     * 初始化默认字段
     */
    public void preInit() {
        SysUser sysUser = ShiroUtils.getUser();

        if (!StringUtils.isNotBlank(this.id)) {
            if (sysUser != null) {
                this.createBy = sysUser.getId();
            }
            this.id = UUID.randomUUID().toString().replaceAll("-", "");
            this.createDate = new Date();
            this.updateDate = new Date();
            this.setDelFlag(Constants.DEL_FLAG);
        } else {
            if (sysUser != null) {
                this.updateBy = sysUser.getId();
            }
            this.updateDate = new Date();
        }
    }

    /**
     * 判断用户是否超级管理员
     * @return
     */
    public boolean isSuperAdmin(){

        if (StringUtils.isNotBlank(this.id)){
            if(this.id.equals(Constants.SUPER_ADMIN)){
                return true;
            }
        }
        return false;
    }
}
